const router = require('koa-router')()
const Mock = require('mockjs')

let { list } = Mock.mock({
  "list|10": [{
    "id": "@id",
    "name": "@cname",
    "word": "@cword(10, 20)",
    "image": "@image(50x50, @color)"
  }]
})

router.get('/list', ctx => {
  const { name } = ctx.request.query
  ctx.body = {
    code: 200,
    data: list.filter(v => v.name.includes(name))
  }
})

router.delete('/list', ctx => {
  const { id } = ctx.request.query;

  list = list.filter(v => v.id !== id)

  ctx.body = {
    code: 200,
    message: '删除成功'
  }
})

router.post('/list', ctx => {
  const { name, image, word } = ctx.request.body;

  const data = {
    name, image, word,
    id: Mock.mock("@id")
  }

  list.unshift(data);

  ctx.body = {
    code: 200,
    message: '添加成功'
  }

})

router.post('/list/edit', ctx => {
  const { name, image, word, id } = ctx.request.body;
  const item = list.find(v => v.id === id)

  item.name = name
  item.image = image
  item.word = word

  ctx.body = {
    code: 200,
    message: '修改成功'
  }
})

module.exports = router
